package com.szly.phm.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.szly.phm.common.entity.dto.simulation.SimulationAlgorithmDTO;
import com.szly.phm.common.entity.dto.simulation.SimulationDTO;
import com.szly.phm.common.entity.dto.user.LoginUserDTO;
import com.szly.phm.common.entity.po.*;
import com.szly.phm.common.entity.result.CodeStatusEnum;
import com.szly.phm.common.entity.result.PageResult;
import com.szly.phm.common.entity.result.RootResponse;
import com.szly.phm.common.entity.vo.simulation.RulSimulationVO;
import com.szly.phm.common.entity.vo.simulation.SimulationVO;
import com.szly.phm.common.mapper.*;
import com.szly.phm.common.service.ISimulationService;
import com.szly.phm.common.utils.BeanCopyUtils;
import com.szly.phm.common.utils.HandleUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * phm
 *
 * @author: git.jas0nch
 * date: 2021-10-18
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SimulationImpl extends ServiceImpl<TaSimulationDataMapper, TaSimulationData> implements ISimulationService {
    private final TaSimulationDataMapper taSimulationDataMapper;
    private final TaRulSimulationDataMapper taRulSimulationDataMapper;
    private final RaRadarMapper raRadarMapper;
    private final FiRadarTypeMapper raServerTypeMapper;
    private final RaKeyPartsModelMapper raKeyPartsModelMapper;

    private final IndexResultMapper indexResultMapper;



    @Override
    public RootResponse simulationData(SimulationDTO createMajorDTO, LoginUserDTO userDTO) {
        var taSimulationData=new TaSimulationData();
        BeanCopyUtils.copy(createMajorDTO,taSimulationData);

        if(createMajorDTO.getId()==null){
            var wrapper1 = new LambdaQueryWrapper<TaSimulationData>();
            wrapper1.eq(TaSimulationData::getIsSoftDelete, false);
            wrapper1.le(TaSimulationData::getStartDataTime,new Date());
            wrapper1.ge(TaSimulationData::getEndDataTime,new Date());
            wrapper1.eq(TaSimulationData::getRadarId,createMajorDTO.getRadarId());
           var taSimulationData1= taSimulationDataMapper.selectList(wrapper1);
           if(taSimulationData1.size()>0){
               RootResponse.success("不能同时开始仿真！！！");
           }
            taSimulationData.setIsSoftDelete(false);
            taSimulationData.setCreator("admin");
            taSimulationData.setCreateDateTime(new Date());
            taSimulationData.setId(HandleUtils.UUID32());
            taSimulationDataMapper.insert(taSimulationData);

        }else {
//           var taSimulationData1= taSimulationDataMapper.selectById(createMajorDTO.getId());
//           if(!taSimulationData1.getCreator().equals(userDTO.getUsername())){
//               RootResponse.success("非本人不能操作！！！");
//           }
            taSimulationData.setIsSoftDelete(false);
            taSimulationData.setUpdater("systemUser");
            taSimulationData.setUpdateDateTime(new Date());
            taSimulationData.setId(createMajorDTO.getId());
            taSimulationDataMapper.updateById(taSimulationData);
        }
        return RootResponse.success("操作成功！！！");
    }

    @Override
    public RootResponse selectSimulationData() {
     var newList=new ArrayList<SimulationVO>();
        var wrapper1 = new LambdaQueryWrapper<TaSimulationData>();
        wrapper1.eq(TaSimulationData::getIsSoftDelete, false);
        wrapper1.eq(TaSimulationData::getState,1);
        taSimulationDataMapper.selectList(wrapper1).forEach(s->{
            var simulationVO=new SimulationVO();
            BeanCopyUtils.copy(s,simulationVO);
            var raRadar=raRadarMapper.selectById( s.getRadarId());
            simulationVO.setRadarName(raRadar==null?null:raRadar.getName());
            var raServerType=raServerTypeMapper.selectById( s.getRadarTypeId());
            simulationVO.setRadarTypeName(raServerType==null?null:raServerType.getName());
            newList.add(simulationVO);
        });
        return RootResponse.success(newList);
    }

    @Override
    public RootResponse simulationAlgorithmData(SimulationAlgorithmDTO createMajorDTO, LoginUserDTO userDTO) {
        var taRulSimulationData=new TaRulSimulationData();
        BeanCopyUtils.copy(createMajorDTO,taRulSimulationData);

        if(createMajorDTO.getId()==null){
            var wrapper1 = new LambdaQueryWrapper<TaRulSimulationData>();
            wrapper1.eq(TaRulSimulationData::getIsSoftDelete, false);
            wrapper1.eq(TaRulSimulationData::getState,1);
            var taSimulationData1= taRulSimulationDataMapper.selectList(wrapper1);
            if(taSimulationData1.size()>0){
                RootResponse.success("不能同时开始仿真！！！");
            }
            taRulSimulationData.setIsSoftDelete(false);
            taRulSimulationData.setCreator(userDTO.getUsername());
            taRulSimulationData.setCreateDateTime(new Date());
            taRulSimulationData.setId(HandleUtils.UUID32());
            taRulSimulationDataMapper.insert(taRulSimulationData);

        }else {
            var taRulSimulationData1= taRulSimulationDataMapper.selectById(createMajorDTO.getId());
            if(!taRulSimulationData1.getCreator().equals(userDTO.getUsername())){
                RootResponse.success("非本人不能操作！！！");
            }
            taRulSimulationData.setIsSoftDelete(false);
            taRulSimulationData.setUpdater(userDTO.getUsername());
            taRulSimulationData.setUpdateDateTime(new Date());
            taRulSimulationData.setId(createMajorDTO.getId());
            taRulSimulationDataMapper.updateById(taRulSimulationData);
        }
        return RootResponse.success("操作成功！！！");
    }

    @Override
    public RootResponse selectSimulationAlgorithmData() {
        var newList=new ArrayList<RulSimulationVO>();
        var wrapper1 = new LambdaQueryWrapper<TaRulSimulationData>();
        wrapper1.eq(TaRulSimulationData::getIsSoftDelete, false);
        wrapper1.eq(TaRulSimulationData::getState,1);
        taRulSimulationDataMapper.selectList(wrapper1).forEach(s->{
            var simulationVO=new RulSimulationVO();
            BeanCopyUtils.copy(s,simulationVO);
            var raRadar=raRadarMapper.selectById( s.getRadarId());
            simulationVO.setRadarName(raRadar==null?null:raRadar.getName());
            var raServerType=raServerTypeMapper.selectById( s.getRadarTypeId());
            simulationVO.setRadarTypeName(raServerType==null?null:raServerType.getName());
            var raKeyPartsModel=raKeyPartsModelMapper.selectById( s.getRaKeyPartsModelId());
            simulationVO.setRaKeyPartsModelName(raKeyPartsModel==null?null:raKeyPartsModel.getName());
            newList.add(simulationVO);
        });
        return RootResponse.success(newList);
    }


    /**
     * @description : TODO
     * @param radarId  : 根据雷达id，进行查询indexResult 的信息，返回相关list
     * @return : com.szly.phm.common.entity.result.RootResponse
     * @author : git.jas0nch
     * date : 2022/3/12
     **/
    @Override
    public RootResponse selectSimulationOfIndexResult(String radarId) {

        LambdaQueryWrapper<IndexResult> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(IndexResult::getSiteCode, radarId);
        List<IndexResult> indexResults = indexResultMapper.selectList(wrapper);


        return RootResponse.success(indexResults);
    }

}
